﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Model;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using System.Data;

namespace SMTC.Dao.BaseInfo
{
    public class ProductionUnitProccessDao : BaseDao
    {
        private const string SQL_INSERT_PU = @"INSERT INTO dbo.SMT_ProductionUnit (
	                                            PUID,
	                                            PUType,
	                                            PUCapacity,
	                                            Para,
                                                EffectiveTimeStart,
                                                EffectiveTimeEnd,
	                                            ModifyPerson,
	                                            ModifyDateTime,
                                                PUVersion
                                            ) VALUES ( 
	                                            /* PUID - uniqueidentifier */ @PUID,
	                                            /* PUType - nvarchar(50) */ @PUType,
	                                            /* PUCapacity - int */ @PUCapacity,
	                                            /* Para - float */ @Para,
                                                @EffectiveTimeStart,
                                                @EffectiveTimeEnd,
	                                            @ModifyPerson,
	                                            @ModifyDateTime,
                                                @PUVersion
                                                ) ";

        private const string SQL_UPDATE_PU = @"UPDATE  dbo.SMT_ProductionUnit SET 
							                   PUType= @PUType,
							                   PUCapacity= @PUCapacity,
							                   Para= @Para,
                                               EffectiveTimeStart=@EffectiveTimeStart,
                                               EffectiveTimeEnd=@EffectiveTimeEnd,
							                   ModifyPerson= @ModifyPerson,
							                   ModifyDateTime= @ModifyDateTime,
                                               PUVersion = @PUVersion
							                   WHERE PUID= @PUID";

        private const string SQL_DELETE_PU = @"DELETE FROM dbo.SMT_ProductionUnit WHERE PUID= @PUID";

        private const string SQL_INSERT_COPYVERSION = @"INSERT INTO dbo.SMT_ProductionUnit (
	                                                        PUID,
	                                                        PUType,
	                                                        PUCapacity,
	                                                        Para,
	                                                        ModifyPerson,
	                                                        ModifyDateTime,
	                                                        LineID,
	                                                        EffectiveTimeStart,
	                                                        EffectiveTimeEnd,
	                                                        PUVersion
                                                        ) 
                                                        SELECT NEWID(),
	                                                           PUType,
	                                                           PUCapacity,
	                                                           Para,
	                                                           ModifyPerson,
	                                                           ModifyDateTime,
	                                                           LineID,
	                                                           EffectiveTimeStart,
	                                                           EffectiveTimeEnd,
	                                                           @newVersion FROM dbo.SMT_ProductionUnit
                                                        WHERE PUVersion = @OldVersion";

        private const string SQL_SELECT_VERSION = @"SELECT DISTINCT PUVersion FROM dbo.SMT_ProductionUnit";

        public List<string> GetPUVersion()
        {
            return DBTemplete.ExcuteGetList<string>(CommandType.Text, SQL_SELECT_VERSION);
        }

        public bool CopyPuVersion(string OldVersion, string newVersion)
        {
            DbParameter[] parameter = 
            {
                DBparameterBuild.Build("@OldVersion",OldVersion)
                ,DBparameterBuild.Build("@newVersion",newVersion)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_COPYVERSION, parameter) > 0;
        }

        public bool InsertPU(SMT_ProductionUnitModel PU)
        {
            DbParameter[] parameter = 
            {
                DBparameterBuild.Build("@PUID",Guid.NewGuid())
                ,DBparameterBuild.Build("@PUType",PU.PUType)
                ,DBparameterBuild.Build("@PUCapacity",PU.PUCapacity)
                ,DBparameterBuild.Build("@Para",PU.Para)
                ,DBparameterBuild.Build("@ModifyPerson",PU.ModifyPerson)
                ,DBparameterBuild.Build("@ModifyDateTime",PU.ModifyDateTime)
                ,DBparameterBuild.Build("@EffectiveTimeStart",PU.EffectiveTimeStart)
                ,DBparameterBuild.Build("@EffectiveTimeEnd",PU.EffectiveTimeEnd)
                ,DBparameterBuild.Build("@PUVersion",PU.PUVersion)
            };

            try
            {
                return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_PU, parameter) > 0;
            }
            catch
            {
                return false;
            }

        }

        public bool UpdatePU(SMT_ProductionUnitModel PU)
        {
            DbParameter[] parameter = 
            {
                DBparameterBuild.Build("@PUID",PU.PUID)
                ,DBparameterBuild.Build("@PUType",PU.PUType)
                ,DBparameterBuild.Build("@PUCapacity",PU.PUCapacity)
                ,DBparameterBuild.Build("@Para",PU.Para)
                ,DBparameterBuild.Build("@ModifyPerson",PU.ModifyPerson)
                ,DBparameterBuild.Build("@ModifyDateTime",PU.ModifyDateTime)
                ,DBparameterBuild.Build("@EffectiveTimeStart",PU.EffectiveTimeStart)
                ,DBparameterBuild.Build("@EffectiveTimeEnd",PU.EffectiveTimeEnd)
                ,DBparameterBuild.Build("@PUVersion",PU.PUVersion)
            };

            try
            {
                return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_UPDATE_PU, parameter) > 0;
            }
            catch
            {
                return false;
            }

        }

        public bool DelPUByPUID(string PUID)
        {
            DbParameter[] parameter = 
            {
                DBparameterBuild.Build("@PUID",PUID)
            };

            try
            {
                return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_PU, parameter) > 0;
            }
            catch
            {
                return false;
            }
        }
    }
}
